﻿#include <iostream>
#include "opencv2/core.hpp"
#include "opencv2/video.hpp"
#include "opencv2/highgui.hpp"
#include <opencv2/imgproc/imgproc.hpp>
#include "CollectPicture.h"
#include "Calibration.h"

using namespace std;
using namespace cv;
using namespace Eigen;

#define ORIGPICTURE 0
#define CORRPICTURE 1


Mat srcImage=imread("color.jpg");//读取图像
Mat midImage;//定义临时变量 /*霍夫圆变换参数声明*/
double dp=1.5;//累加器图像的反比分辨率 
double min_dist=0.1;//检测到圆心之间的最小距离 
int param_1=30;//Canny边缘函数的高阈值
int param_2=20;//圆心检测阈值
int min_radius=0,max_radius=100;//能检测到的最小最大圆半径 

void on_Trackbar(int ,void*)
{ /*霍夫圆变换*/ 

	vector<Vec3f> circles;//声明一个向量，保存检测出的圆的圆心坐标和半径
	HoughCircles(midImage, circles,CV_HOUGH_GRADIENT, dp, min_dist, param_1, param_2, min_radius, max_radius); /*把图像复制到srcImage2，让每次回调都重新画图*/
	 Mat srcImage2; 
	srcImage.copyTo(srcImage2); /*依次在图中绘制出圆*/ 
	for (size_t i = 0; i < circles.size(); i++) 
	{ //参数定义
	  Point center(cvRound(circles[i][0]), cvRound(circles[i][1])); 
	  int radius = cvRound(circles[i][2]); 
	  circle(srcImage2, center, 3, Scalar(0, 255, 0), -1, 8, 0);//绘制圆心 
	  circle(srcImage2, center, radius, Scalar(0, 0, 255), 2, 8, 0);//绘制圆轮廓 
	} 

    namedWindow("【效果图】");
    imshow("【效果图】",srcImage2);//显示原始图 
    waitKey(30);


} 


int main()
{
	//calibration calibrationPic;
/*	CollectPicture collectPic;
	calibration calibrationPic;
	vector<Mat> Pictures;
	vector<Mat> CorPictures;
	Mat correct;
	char Select;
	cout << "是否开始采集图片，请输入Y/N.." << endl;
	cin >> Select;
	while (Select == 'Y'|| Select == 'y')
	{
		Pictures.clear();
		cout << "开始采集标定图像，请采集15张图片..." << endl;
		collectPic.CollectPic(Pictures);
		cout << "是否要重新采集？Y/N " << endl;
		cin >> Select;
	}
	cout << "采集图像完成，共采集 " << Pictures.size() << "张图片" << endl;
	cout << "开始处理采集的图像，第一次标定....." << endl;
	calibrationPic.CalibrationPic(Pictures, ORIGPICTURE);
	cout << "第一次标定完成." << endl;

	cout << "开始矫正....." << endl;
	CorPictures.clear();
	calibrationPic.GetCorpictures(Pictures, CorPictures);
	cout << "矫正完成。" << endl;

	cout << "第二次标定....." << endl;
	calibrationPic.CalibrationPic(CorPictures, CORRPICTURE);
	cout << "第二次标定完成." << endl;
	cout << "开始测试结果....." << endl;
	calibrationPic.testResult(CORRPICTURE);
	cout << "结果测试结束." << endl;
*/

   Mat gray;

    namedWindow("【原始图】");
    imshow("【原始图】",srcImage);//显示原始图 
    waitKey(30);

    cvtColor( srcImage, gray, CV_BGR2GRAY );  

    threshold(gray, midImage, 100, 200.0, CV_THRESH_BINARY);//二值化处理 

    namedWindow("【图】");
    imshow("【图】",midImage);//显示原始图 
    waitKey(300);



    Canny(midImage,midImage,50,200,3);//canny边缘检测 

    namedWindow("【效果图】",1); /*创建滑动条*/ 

    createTrackbar("圆心阈值","【效果图】",&param_2,100,on_Trackbar); 
    createTrackbar("最小半径","【效果图】",&min_radius,100,on_Trackbar); 

    on_Trackbar(param_2,0); 
    on_Trackbar(min_radius,0); 

    waitKey(0);


//	Mat jiaozheng = imread("color.jpg"); 
//	Mat midImage, dstImage; 
	//Mat jiaozheng;

//	namedWindow("【原始图】");
//	imshow("【原始图】", jiaozheng); 
//	waitKey(30);

/*    jiaozheng = calibrationPic.CorrectPic(srcImage.clone());
	namedWindow("【原始图1】");
	imshow("【原始图1】", jiaozheng); 
	waitKey(30);    
*/
//	medianBlur(jiaozheng, jiaozheng, 5);
//	cvtColor(jiaozheng, midImage, CV_BGR2GRAY);//转化边缘检测后的图为灰度图 

//	namedWindow("midImage");
//	imshow("midImage", midImage); 
//	waitKey(30);
	
	//GaussianBlur(midImage, midImage, Size(9, 9), 2, 2); 


/*	vector<Vec3f> circles;
	HoughCircles(midImage, circles, CV_HOUGH_GRADIENT, 1, 10, 100, 30, 1, 30); //依次在图中绘制出圆 
	cout << circles.size() << endl;
	for (size_t i = 0; i < circles.size(); i++) 
	{ 
		Point center(cvRound(circles[i][0]), cvRound(circles[i][1])); 
		int radius = cvRound(circles[i][2]); //绘制圆心
		circle(jiaozheng, center, 1, Scalar(0, 255, 0), -1, 8, 0); //yuan xin 
		//circle(jiaozheng, center, radius, Scalar(155, 50, 255), 3, 8, 0); //lun kuo
	} 

	namedWindow("【图】");
	imshow("【图】", jiaozheng); 
	waitKey(300000);

	//cout << "开始测试结果....." << endl;
	//calibrationPic.testResult(CORRPICTURE);
	//cout << "结果测试结束." << endl;
	char dest;
	cin >> dest;

    system("pause");*/
	return 0;
}
